﻿Imports System.IO
Imports System.Threading
Imports TyranntEngine

Public Class transferForm

    Private _jobList As GameFileCollection
    Private _totalSize As Long = 0
    Private _token As String = ""
    Private WithEvents _client As New TyranntWelcomeService.WelcomeClient

    Private WithEvents Worker As New System.ComponentModel.BackgroundWorker

    Private cancelled As Boolean = False
    Private errDlg As New ErrorDlg

    Private Sub transferForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        If IsNothing(_jobList) Then
            MessageBox.Show("You must set a job list before showing this dialog", "Transfer failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.Close()
        End If
        If _token = "" Then
            MessageBox.Show("You must set the user token before showing this dialog", "Transfer failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.Close()
        End If
        Worker.WorkerReportsProgress = True
        Worker.WorkerSupportsCancellation = True
        Worker.RunWorkerAsync()
    End Sub

    Public WriteOnly Property JobList() As GameFileCollection
        Set(ByVal value As GameFileCollection)
            _jobList = value
            ProcessJobList()
        End Set
    End Property

    Public WriteOnly Property Token() As String
        Set(ByVal value As String)
            _token = value
        End Set
    End Property

    Private Sub ProcessJobList()
        _totalSize = 0
        For Each job As GameFile In _jobList
            _totalSize += job.Size
        Next

        totalProgressBar.Maximum = CInt(_totalSize)
    End Sub

    Private Sub TestWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker.DoWork
        For Each job As GameFile In _jobList
            Select Case job.Action
                Case "upload"
                    Worker.ReportProgress(0, "0" + Chr(254) + "Uploading " + job.Filename)
                    ' First we need to add the pre-file information
                    Dim result As String = _client.PreloadFile(_token, job.Filename, job.Size)
                    If result <> "" Then
                        MessageBox.Show(result, "Preload File failed [" + job.Filename + "]", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        Exit Sub
                    End If
                    Dim sr As New FileStream(job.Filename, FileMode.Open)
                    Dim br As New BinaryReader(sr)

                    If Worker.CancellationPending = True Then
                        Exit Sub
                    End If
                    result = _client.PutFile(br.BaseStream)
                    If result <> "" Then
                        MessageBox.Show(result, "Upload file failed [" + job.Filename + "]", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        Exit Sub
                    End If
                    sr.Close()
                    Worker.ReportProgress(0, job.Size.ToString + Chr(254) + "Uploading " + job.Filename)
                Case "delete"
                    Worker.ReportProgress(0, "0" + Chr(254) + "Deleting " + job.Filename)
                    _client.DeleteFile(_token, job.Filename)
                    Worker.ReportProgress(0, job.Size.ToString + Chr(254) + "Deleting " + job.Filename)
            End Select
            If Worker.CancellationPending = True Then
                Exit Sub
            End If
        Next
    End Sub

    Private Sub TestWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles Worker.ProgressChanged
        Dim bits() As String = e.UserState.ToString.Split(Chr(254))
        Dim bytesSoFar As Integer
        If Integer.TryParse(bits(0), bytesSoFar) Then
            totalProgressBar.Value += bytesSoFar
        End If
        actionLabel.Text = bits(1)
    End Sub

    Private Sub Worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles Worker.RunWorkerCompleted
        Me.Close()
    End Sub

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Worker.CancelAsync()
        cancelled = True
    End Sub
End Class
